package mil.af.amc.adpetracker.dao.hibernate;

import mil.af.amc.adpetracker.dao.AccountDao;
import mil.af.amc.adpetracker.model.Account;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Please Fill in this Javadoc
 *
 * @author TSgt Jason Ferguson
 */
@Service("accountDao")
public class HibernateAccountDao extends HibernateGenericDao<Account> implements AccountDao {

    public HibernateAccountDao() {
        super(Account.class);
    }

    @SuppressWarnings({"unchecked"})
    public Account getByUsername(String username) {


        DetachedCriteria crit = DetachedCriteria.forClass(Account.class);
        crit.add(Restrictions.eq("username", username));

        List<Account> results = (List<Account>) getHibernateTemplate().findByCriteria(crit);

        if (results.size() < 1) {
            return null;
        } else {
            getHibernateTemplate().initialize(results);
            return results.get(0);
        }
    }
}
